{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "data85014\r\n"
     ]
    }
   ],
   "source": [
    "# 查看当前挂载的数据集目录, 该目录下的变更重启环境后会自动还原\n",
    "# View dataset directory. \n",
    "# This directory will be recovered automatically after resetting environment. \n",
    "!ls /home/aistudio/data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# 查看工作区文件, 该目录下的变更将会持久保存. 请及时清理不必要的文件, 避免加载过慢.\n",
    "# View personal work directory. \n",
    "# All changes under this directory will be kept even after reset. \n",
    "# Please clean unnecessary files in time to speed up environment loading. \n",
    "!ls /home/aistudio/work"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "mkdir: cannot create directory ‘/home/aistudio/external-libraries’: File exists\n",
      "Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple\n",
      "Collecting beautifulsoup4\n",
      "  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/9c/d8/909c4089dbe4ade9f9705f143c9f13f065049a9d5e7d34c828aefdd0a97c/beautifulsoup4-4.11.1-py3-none-any.whl (128 kB)\n",
      "\u001b[2K     \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m128.2/128.2 kB\u001b[0m \u001b[31m469.7 kB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0ma \u001b[36m0:00:01\u001b[0m\n",
      "\u001b[?25hCollecting soupsieve>1.2\n",
      "  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/16/e3/4ad79882b92617e3a4a0df1960d6bce08edfb637737ac5c3f3ba29022e25/soupsieve-2.3.2.post1-py3-none-any.whl (37 kB)\n",
      "Installing collected packages: soupsieve, beautifulsoup4\n",
      "Successfully installed beautifulsoup4-4.11.1 soupsieve-2.3.2.post1\n",
      "\u001b[33mWARNING: Target directory /home/aistudio/external-libraries/soupsieve-2.3.2.post1.dist-info already exists. Specify --upgrade to force replacement.\u001b[0m\u001b[33m\n",
      "\u001b[0m\u001b[33mWARNING: Target directory /home/aistudio/external-libraries/bs4 already exists. Specify --upgrade to force replacement.\u001b[0m\u001b[33m\n",
      "\u001b[0m\u001b[33mWARNING: Target directory /home/aistudio/external-libraries/beautifulsoup4-4.11.1.dist-info already exists. Specify --upgrade to force replacement.\u001b[0m\u001b[33m\n",
      "\u001b[0m\u001b[33mWARNING: Target directory /home/aistudio/external-libraries/soupsieve already exists. Specify --upgrade to force replacement.\u001b[0m\u001b[33m\n",
      "\u001b[0m"
     ]
    }
   ],
   "source": [
    "# 如果需要进行持久化安装, 需要使用持久化路径, 如下方代码示例:\n",
    "# If a persistence installation is required, \n",
    "# you need to use the persistence path as the following: \n",
    "!mkdir /home/aistudio/external-libraries\n",
    "!pip install beautifulsoup4 -t /home/aistudio/external-libraries"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# 同时添加如下代码, 这样每次环境(kernel)启动的时候只要运行下方代码即可: \n",
    "# Also add the following code, \n",
    "# so that every time the environment (kernel) starts, \n",
    "# just run the following code: \n",
    "import sys \n",
    "sys.path.append('/home/aistudio/external-libraries')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "请点击[此处](https://ai.baidu.com/docs#/AIStudio_Project_Notebook/a38e5576)查看本环境基本用法.  <br>\n",
    "Please click [here ](https://ai.baidu.com/docs#/AIStudio_Project_Notebook/a38e5576) for more detailed instructions. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/__init__.py:107: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working\n",
      "  from collections import MutableMapping\n",
      "/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/rcsetup.py:20: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working\n",
      "  from collections import Iterable, Mapping\n",
      "/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/colors.py:53: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working\n",
      "  from collections import Sized\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'2.3.0'"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import paddle\r\n",
    "import numpy as np\r\n",
    "import matplotlib.pyplot as plt\r\n",
    "import cv2\r\n",
    "paddle.__version__"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "item  143/2421 [>.............................] - ETA: 2s - 932us/it"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Cache file /home/aistudio/.cache/paddle/dataset/mnist/train-images-idx3-ubyte.gz not found, downloading https://dataset.bj.bcebos.com/mnist/train-images-idx3-ubyte.gz \n",
      "Begin to download\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "item  460/2421 [====>.........................] - ETA: 2s - 1ms/itemitem  839/2421 [=========>....................] - ETA: 1s - 1ms/\n",
      "item 8/8 [============================>.] - ETA: 0s - 2ms/it"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n",
      "Download finished\n",
      "Cache file /home/aistudio/.cache/paddle/dataset/mnist/train-labels-idx1-ubyte.gz not found, downloading https://dataset.bj.bcebos.com/mnist/train-labels-idx1-ubyte.gz \n",
      "Begin to download\n",
      "\n",
      "Download finished\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "item 170/403 [===========>..................] - ETA: 0s - 910us/it"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Cache file /home/aistudio/.cache/paddle/dataset/mnist/t10k-images-idx3-ubyte.gz not found, downloading https://dataset.bj.bcebos.com/mnist/t10k-images-idx3-ubyte.gz \n",
      "Begin to download\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "item 306/403 [=====================>........] - ETA: 0s - 1ms/itemitem 1/2 [=============>................] - ETA: 0s - 1ms/it\n",
      "item 2/2 [===========================>..] - ETA: 0s - 3ms/it"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "\n",
      "Download finished\n",
      "Cache file /home/aistudio/.cache/paddle/dataset/mnist/t10k-labels-idx1-ubyte.gz not found, downloading https://dataset.bj.bcebos.com/mnist/t10k-labels-idx1-ubyte.gz \n",
      "Begin to download\n",
      "\n",
      "Download finished\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "训练集样本量：60000, 验证集样本量10000\n",
      "[[[-1.         -1.         -1.         -1.         -1.\n",
      "   -1.         -1.         -1.         -1.         -1.\n",
      "   -1.         -1.         -1.         -1.         -1.\n",
      "   -1.         -1.         -1.         -1.         -1.\n",
      "   -1.         -1.         -1.         -1.         -1.\n",
      "   -1.         -1.         -1.        ]\n",
      "  [-1.         -1.         -1.         -1.         -1.\n",
      "   -1.         -1.         -1.         -1.         -1.\n",
      "   -1.         -1.         -1.         -1.         -1.\n",
      "   -1.         -1.         -1.         -1.         -1.\n",
      "   -1.         -1.         -1.         -1.         -1.\n",
      "   -1.         -1.         -1.        ]\n",
      "  [-1.         -1.         -1.         -1.         -1.\n",
      "   -1.         -1.         -1.         -1.         -1.\n",
      "   -1.         -1.         -1.         -1.         -1.\n",
      "   -1.         -1.         -1.         -1.         -1.\n",
      "   -1.         -1.         -1.         -1.         -1.\n",
      "   -1.         -1.         -1.        ]\n",
      "  [-1.         -1.         -1.         -1.         -1.\n",
      "   -1.         -1.         -1.         -1.         -1.\n",
      "   -1.         -1.         -1.         -1.         -1.\n",
      "   -1.         -1.         -1.         -1.         -1.\n",
      "   -1.         -1.         -1.         -1.         -1.\n",
      "   -1.         -1.         -1.        ]\n",
      "  [-1.         -1.         -1.         -1.         -1.\n",
      "   -1.         -1.         -1.         -1.         -1.\n",
      "   -1.         -1.         -1.         -1.         -1.\n",
      "   -1.         -1.         -1.         -1.         -1.\n",
      "   -1.         -1.         -1.         -1.         -1.\n",
      "   -1.         -1.         -1.        ]\n",
      "  [-1.         -1.         -1.         -1.         -1.\n",
      "   -1.         -1.         -1.         -1.         -1.\n",
      "   -1.         -1.         -0.9764706  -0.85882354 -0.85882354\n",
      "   -0.85882354 -0.01176471  0.06666667  0.37254903 -0.79607844\n",
      "    0.3019608   1.          0.9372549  -0.00392157 -1.\n",
      "   -1.         -1.         -1.        ]\n",
      "  [-1.         -1.         -1.         -1.         -1.\n",
      "   -1.         -1.         -1.         -0.7647059  -0.7176471\n",
      "   -0.2627451   0.20784314  0.33333334  0.9843137   0.9843137\n",
      "    0.9843137   0.9843137   0.9843137   0.7647059   0.34901962\n",
      "    0.9843137   0.8980392   0.5294118  -0.49803922 -1.\n",
      "   -1.         -1.         -1.        ]\n",
      "  [-1.         -1.         -1.         -1.         -1.\n",
      "   -1.         -1.         -0.6156863   0.8666667   0.9843137\n",
      "    0.9843137   0.9843137   0.9843137   0.9843137   0.9843137\n",
      "    0.9843137   0.9843137   0.96862745 -0.27058825 -0.35686275\n",
      "   -0.35686275 -0.56078434 -0.69411767 -1.         -1.\n",
      "   -1.         -1.         -1.        ]\n",
      "  [-1.         -1.         -1.         -1.         -1.\n",
      "   -1.         -1.         -0.85882354  0.7176471   0.9843137\n",
      "    0.9843137   0.9843137   0.9843137   0.9843137   0.5529412\n",
      "    0.42745098  0.9372549   0.8901961  -1.         -1.\n",
      "   -1.         -1.         -1.         -1.         -1.\n",
      "   -1.         -1.         -1.        ]\n",
      "  [-1.         -1.         -1.         -1.         -1.\n",
      "   -1.         -1.         -1.         -0.37254903  0.22352941\n",
      "   -0.16078432  0.9843137   0.9843137   0.60784316 -0.9137255\n",
      "   -1.         -0.6627451   0.20784314 -1.         -1.\n",
      "   -1.         -1.         -1.         -1.         -1.\n",
      "   -1.         -1.         -1.        ]\n",
      "  [-1.         -1.         -1.         -1.         -1.\n",
      "   -1.         -1.         -1.         -1.         -0.8901961\n",
      "   -0.99215686  0.20784314  0.9843137  -0.29411766 -1.\n",
      "   -1.         -1.         -1.         -1.         -1.\n",
      "   -1.         -1.         -1.         -1.         -1.\n",
      "   -1.         -1.         -1.        ]\n",
      "  [-1.         -1.         -1.         -1.         -1.\n",
      "   -1.         -1.         -1.         -1.         -1.\n",
      "   -1.          0.09019608  0.9843137   0.49019608 -0.9843137\n",
      "   -1.         -1.         -1.         -1.         -1.\n",
      "   -1.         -1.         -1.         -1.         -1.\n",
      "   -1.         -1.         -1.        ]\n",
      "  [-1.         -1.         -1.         -1.         -1.\n",
      "   -1.         -1.         -1.         -1.         -1.\n",
      "   -1.         -0.9137255   0.49019608  0.9843137  -0.4509804\n",
      "   -1.         -1.         -1.         -1.         -1.\n",
      "   -1.         -1.         -1.         -1.         -1.\n",
      "   -1.         -1.         -1.        ]\n",
      "  [-1.         -1.         -1.         -1.         -1.\n",
      "   -1.         -1.         -1.         -1.         -1.\n",
      "   -1.         -1.         -0.7254902   0.8901961   0.7647059\n",
      "    0.25490198 -0.15294118 -0.99215686 -1.         -1.\n",
      "   -1.         -1.         -1.         -1.         -1.\n",
      "   -1.         -1.         -1.        ]\n",
      "  [-1.         -1.         -1.         -1.         -1.\n",
      "   -1.         -1.         -1.         -1.         -1.\n",
      "   -1.         -1.         -1.         -0.3647059   0.88235295\n",
      "    0.9843137   0.9843137  -0.06666667 -0.8039216  -1.\n",
      "   -1.         -1.         -1.         -1.         -1.\n",
      "   -1.         -1.         -1.        ]\n",
      "  [-1.         -1.         -1.         -1.         -1.\n",
      "   -1.         -1.         -1.         -1.         -1.\n",
      "   -1.         -1.         -1.         -1.         -0.64705884\n",
      "    0.45882353  0.9843137   0.9843137   0.1764706  -0.7882353\n",
      "   -1.         -1.         -1.         -1.         -1.\n",
      "   -1.         -1.         -1.        ]\n",
      "  [-1.         -1.         -1.         -1.         -1.\n",
      "   -1.         -1.         -1.         -1.         -1.\n",
      "   -1.         -1.         -1.         -1.         -1.\n",
      "   -0.8745098  -0.27058825  0.9764706   0.9843137   0.46666667\n",
      "   -1.         -1.         -1.         -1.         -1.\n",
      "   -1.         -1.         -1.        ]\n",
      "  [-1.         -1.         -1.         -1.         -1.\n",
      "   -1.         -1.         -1.         -1.         -1.\n",
      "   -1.         -1.         -1.         -1.         -1.\n",
      "   -1.         -1.          0.9529412   0.9843137   0.9529412\n",
      "   -0.49803922 -1.         -1.         -1.         -1.\n",
      "   -1.         -1.         -1.        ]\n",
      "  [-1.         -1.         -1.         -1.         -1.\n",
      "   -1.         -1.         -1.         -1.         -1.\n",
      "   -1.         -1.         -1.         -1.         -0.6392157\n",
      "    0.01960784  0.43529412  0.9843137   0.9843137   0.62352943\n",
      "   -0.9843137  -1.         -1.         -1.         -1.\n",
      "   -1.         -1.         -1.        ]\n",
      "  [-1.         -1.         -1.         -1.         -1.\n",
      "   -1.         -1.         -1.         -1.         -1.\n",
      "   -1.         -1.         -0.69411767  0.16078432  0.79607844\n",
      "    0.9843137   0.9843137   0.9843137   0.9607843   0.42745098\n",
      "   -1.         -1.         -1.         -1.         -1.\n",
      "   -1.         -1.         -1.        ]\n",
      "  [-1.         -1.         -1.         -1.         -1.\n",
      "   -1.         -1.         -1.         -1.         -1.\n",
      "   -0.8117647  -0.10588235  0.73333335  0.9843137   0.9843137\n",
      "    0.9843137   0.9843137   0.5764706  -0.3882353  -1.\n",
      "   -1.         -1.         -1.         -1.         -1.\n",
      "   -1.         -1.         -1.        ]\n",
      "  [-1.         -1.         -1.         -1.         -1.\n",
      "   -1.         -1.         -1.         -0.81960785 -0.48235294\n",
      "    0.67058825  0.9843137   0.9843137   0.9843137   0.9843137\n",
      "    0.5529412  -0.3647059  -0.9843137  -1.         -1.\n",
      "   -1.         -1.         -1.         -1.         -1.\n",
      "   -1.         -1.         -1.        ]\n",
      "  [-1.         -1.         -1.         -1.         -1.\n",
      "   -1.         -0.85882354  0.34117648  0.7176471   0.9843137\n",
      "    0.9843137   0.9843137   0.9843137   0.5294118  -0.37254903\n",
      "   -0.92941177 -1.         -1.         -1.         -1.\n",
      "   -1.         -1.         -1.         -1.         -1.\n",
      "   -1.         -1.         -1.        ]\n",
      "  [-1.         -1.         -1.         -1.         -0.5686275\n",
      "    0.34901962  0.77254903  0.9843137   0.9843137   0.9843137\n",
      "    0.9843137   0.9137255   0.04313726 -0.9137255  -1.\n",
      "   -1.         -1.         -1.         -1.         -1.\n",
      "   -1.         -1.         -1.         -1.         -1.\n",
      "   -1.         -1.         -1.        ]\n",
      "  [-1.         -1.         -1.         -1.          0.06666667\n",
      "    0.9843137   0.9843137   0.9843137   0.6627451   0.05882353\n",
      "    0.03529412 -0.8745098  -1.         -1.         -1.\n",
      "   -1.         -1.         -1.         -1.         -1.\n",
      "   -1.         -1.         -1.         -1.         -1.\n",
      "   -1.         -1.         -1.        ]\n",
      "  [-1.         -1.         -1.         -1.         -1.\n",
      "   -1.         -1.         -1.         -1.         -1.\n",
      "   -1.         -1.         -1.         -1.         -1.\n",
      "   -1.         -1.         -1.         -1.         -1.\n",
      "   -1.         -1.         -1.         -1.         -1.\n",
      "   -1.         -1.         -1.        ]\n",
      "  [-1.         -1.         -1.         -1.         -1.\n",
      "   -1.         -1.         -1.         -1.         -1.\n",
      "   -1.         -1.         -1.         -1.         -1.\n",
      "   -1.         -1.         -1.         -1.         -1.\n",
      "   -1.         -1.         -1.         -1.         -1.\n",
      "   -1.         -1.         -1.        ]\n",
      "  [-1.         -1.         -1.         -1.         -1.\n",
      "   -1.         -1.         -1.         -1.         -1.\n",
      "   -1.         -1.         -1.         -1.         -1.\n",
      "   -1.         -1.         -1.         -1.         -1.\n",
      "   -1.         -1.         -1.         -1.         -1.\n",
      "   -1.         -1.         -1.        ]]]\n",
      "[5]\n"
     ]
    },
    {
     "ename": "error",
     "evalue": "OpenCV(4.1.1) /io/opencv/modules/imgcodecs/src/loadsave.cpp:667: error: (-215:Assertion failed) image.channels() == 1 || image.channels() == 3 || image.channels() == 4 in function 'imwrite_'\n",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31merror\u001b[0m                                     Traceback (most recent call last)",
      "\u001b[0;32m/tmp/ipykernel_98/2067113423.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m      8\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtrain_dataset\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      9\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 10\u001b[0;31m \u001b[0mcv2\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mimwrite\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"C:/1.PNG\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtrain_dataset\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     11\u001b[0m \u001b[0mplt\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfigure\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     12\u001b[0m \u001b[0mplt\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mimshow\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtrain_dataset\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mreshape\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m28\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m28\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mcmap\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mplt\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mcm\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mbinary\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31merror\u001b[0m: OpenCV(4.1.1) /io/opencv/modules/imgcodecs/src/loadsave.cpp:667: error: (-215:Assertion failed) image.channels() == 1 || image.channels() == 3 || image.channels() == 4 in function 'imwrite_'\n"
     ]
    }
   ],
   "source": [
    "import paddle.vision.transforms as T\r\n",
    "transform = T.Normalize(mean=[127.5], std=[127.5])\r\n",
    "train_dataset = paddle.vision.datasets.MNIST(mode='train', transform=transform)\r\n",
    "eval_dataset = paddle.vision.datasets.MNIST(mode='test',transform=transform)\r\n",
    "\r\n",
    "print('训练集样本量：{}, 验证集样本量{}'.format(len(train_dataset),len(eval_dataset)))\r\n",
    "print(train_dataset[0][0])\r\n",
    "print(train_dataset[0][1])\r\n",
    "\r\n",
    "cv2.imwrite(\"C:/1.PNG\", train_dataset[0][0])\r\n",
    "plt.figure()\r\n",
    "plt.imshow(train_dataset[0][0].reshape([28,28]),cmap=plt.cm.binary)\r\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "network = paddle.nn.Sequential(\r\n",
    "    paddle.nn.Conv2D(1,10,5),\r\n",
    "    paddle.nn.MaxPool2D(3,2),\r\n",
    "    paddle.nn.Conv2D(10,20,5),\r\n",
    "    paddle.nn.MaxPool2D(3,2),\r\n",
    "    paddle.nn.Flatten(),\r\n",
    "    paddle.nn.Linear(180,64),\r\n",
    "    paddle.nn.ReLU(),\r\n",
    "    paddle.nn.Linear(64,10)\r\n",
    "\r\n",
    ")\r\n",
    "model = paddle.Model(network)\r\n",
    "model.summary((1,1,28,28))\r\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "model.prepare(paddle.optimizer.Adam(learning_rate=0.001, parameters=network.parameters()),\r\n",
    "                paddle.nn.CrossEntropyLoss(), paddle.metric.Accuracy())\r\n",
    "model.fit(train_dataset,eval_dataset,epochs=5,batch_size=64,verbose=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "result = model.evaluate(eval_dataset,verbose=1)\r\n",
    "print(result)\r\n",
    "res = model.predict(eval_dataset,verbose=1)\r\n",
    "print(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def show_img(img,predict):\r\n",
    "    plt.figure()\r\n",
    "    plt.title(\"predict:{}\".format(predict))\r\n",
    "    plt.imshow(img.reshape([28,28]),cmap=plt.cm.binary)\r\n",
    "    plt.show()\r\n",
    "\r\n",
    "indexs = [1,26,56,111]\r\n",
    "\r\n",
    "for idx in indexs:\r\n",
    "    show_img(eval_dataset[idx][0], res[0][idx].argmax())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "model.save('snap/mnist',training=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "from paddle.static import InputSpec\r\n",
    "model2 = paddle.Model(network,inputs=[InputSpec(shape=[-1, 28, 28], dtype='float32', name='image')])\r\n",
    "model2.load('snap/mnist')\r\n",
    "model2.prepare(paddle.optimizer.Adam(learning_rate=0.001, parameters=network.parameters()),\r\n",
    "                paddle.nn.CrossEntropyLoss(), paddle.metric.Accuracy())\r\n",
    "model2.fit(train_dataset,eval_dataset,epochs=5,batch_size=64,verbose=1)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "py35-paddle1.2.0"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
